package com.haixinga.commbiz.mgr.ctrl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haixinga.commbiz.commons.entity.SysEntitlement;
import com.haixinga.commbiz.commons.service.SysEntitlementService;
import com.haixinga.commbiz.commons.vo.ApiRes;
import org.springframework.beans.factory.annotation.Autowired;
import com.haixinga.commbiz.commons.biz.ITokenService;
import com.haixinga.commbiz.commons.constants.CS;
import com.haixinga.commbiz.commons.entity.SysUser;
import com.haixinga.commbiz.commons.exception.BizException;
import com.haixinga.commbiz.commons.utils.StringKit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.haixinga.commbiz.commons.vo.TreeDataBuilder;

import java.util.*;

/** 当前登录者的信息相关接口 **/
@RestController
@RequestMapping("api/current")
public class CurrentUserController extends CommonCtrl{

	@Autowired
    SysEntitlementService sysEntitlementService;


	/** 查询当前登录者用户信息 */
	@RequestMapping(value="/user", method = RequestMethod.GET)
	public ApiRes currentUserInfo() {

		///当前用户信息
		SysUser user = getCurrentUser().getSysUser();

		//1. 当前用户所有权限ID集合
		List<String> entIdList = getCurrentUser().getEntIdList();

		List<SysEntitlement> allMenuList = new ArrayList<>();    //所有菜单集合
		Map<String, SysEntitlement> allMenuMap = new HashMap<>();   //所有菜单map
		List<SysEntitlement> leftMenuList = new ArrayList<>();  //显示的菜单集合

		//2. 查询出用户所有菜单集合
		if(entIdList != null && !entIdList.isEmpty()){
			allMenuList = sysEntitlementService.list(SysEntitlement.gw()
					.in(SysEntitlement::getEntId, entIdList)
					.in(SysEntitlement::getEntType, Arrays.asList(CS.ENT_TYPE.LEFT_MENU, CS.ENT_TYPE.OTHER_MENU)));
		}

		//3. 组装成 Map格式， 方便根据ID查询到菜单集合
		allMenuList.stream().forEach(menu -> {
			allMenuMap.put(menu.getEntId(), menu);
			if(menu.getEntType() == CS.ENT_TYPE.LEFT_MENU){  //左侧菜单
				leftMenuList.add(menu);
			}
		});

		//4. 转换为json树状结构
		JSONArray jsonArray = (JSONArray) JSONArray.toJSON(leftMenuList);
		List<JSONObject> leftMenuTree = new TreeDataBuilder(jsonArray,
				"entId", "pid", "subMenus", "entSort", true)
				.buildTreeObject();

		//1. 所有权限ID集合
		user.addExt("entIdList",entIdList);
		user.addExt("leftMenuTree", leftMenuTree);
		user.addExt("allMenuMap", allMenuMap);





		return ApiRes.ok(getCurrentUser().getSysUser());
	}


	/** 修改个人信息 */
	@RequestMapping(value="/user", method = RequestMethod.PUT)
	public ApiRes modifyCurrentUserInfo() {

		//修改头像
		String avatarUrl = getValStringRequired("avatarUrl");
//
//		SysUser updateRecord = new SysUser();
//		updateRecord.setSysUserId(getCurrentUser().getUserId());
//		updateRecord.setAvatarUrl(avatarUrl);
//
//		sysUserService.updateById(updateRecord);
		return ApiRes.ok();
	}


	/** 重置密码 */
	@RequestMapping(value="resetPwd", method = RequestMethod.PUT)
	public ApiRes resetPwd() throws BizException{

		String oldPwd = StringKit.base64Decode(getValStringRequired("iop")); //i old pwd
		String newPwd = StringKit.base64Decode(getValStringRequired("inp")); //i new pwd

//
//		//验证当前密码是否正确
//		if(!sysUserAuthService.validateCurrentUserPwd(oldPwd)){
//			throw new BizException("密码验证失败！");
//		}
//
//		//重置密码
//		sysUserAuthService.resetPwd(getCurrentUser().getUserId(), newPwd);

		//调用登出接口
		return logout();
	}

	/** 登出 */
	@RequestMapping(value="logout", method = RequestMethod.POST)
	public ApiRes logout() throws BizException{

		ITokenService.removeIToken(getCurrentUser().getIToken(), getCurrentUser().getUserId());
		return ApiRes.ok();
	}



}
